import axios from "axios";

const service = axios.create({
  baseURL: 'http://localhost:8080/',
  timeout: 10000,
  withCredentials: true,
})

service.interceptors.request.use(
  config => {

    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  response => {
    console.log(response)
    const {data} = response;
    //未登录
    if(data.code === 401){
      if(!response.request.responseURL.includes("user/current") &&
      !window.location.pathname.includes("/user/login")){
        window.location.href = `/user/login?redirect=${window.location.href}`;
      }

    }
    return response;
  },
  error => {
    return Promise.reject(error);
  }
);

export default service;
